package com.yunda.nebula.gateway.listener.nacos;

import com.alibaba.cloud.nacos.NacosConfigProperties;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.yunda.nebula.gateway.core.entity.ConfigData;
import com.yunda.nebula.gateway.listener.DataChangeListener;
import com.yunda.nebula.gateway.service.ITbGatewayAppService;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * @author zhiguo.dong
 * @date 2020/9/24
 */
@Slf4j
public class NacosDataChangeListener implements DataChangeListener {

    private NacosConfigProperties nacosConfigProperties;

    private ITbGatewayAppService appService;

    public NacosDataChangeListener(ITbGatewayAppService appService, NacosConfigProperties nacosConfigProperties) {
        this.appService = appService;
        this.nacosConfigProperties = nacosConfigProperties;
    }


    @Override
    public void onCreate(Object appid)  {
        ConfigService configService = nacosConfigProperties.configServiceInstance();
        String app = String.valueOf(appid);

        List<String> appList = appService.listApps();
        try {
            configService.publishConfig("chq-jc-gateway-appid",nacosConfigProperties.getGroup(), JSONObject.toJSONString(appList));

            ConfigData configData = appService.fetch(app);
            configService.publishConfig("chq-jc-gateway-"+app,nacosConfigProperties.getGroup(), JSONObject.toJSONString(configData));

        } catch (NacosException e) {
            //ignore
        }

    }

    @Override
    public void onUpdate(Object appid) {
        ConfigService configService = nacosConfigProperties.configServiceInstance();
        String app = String.valueOf(appid);
        try {
            ConfigData configData = appService.fetch(app);
            if (Objects.nonNull(configData)){
                configService.publishConfig("chq-jc-gateway-"+app,nacosConfigProperties.getGroup(), JSONObject.toJSONString(configData));
            }else {
                List<String> appList = appService.listApps();
                configService.publishConfig("chq-jc-gateway-appid",nacosConfigProperties.getGroup(), JSONObject.toJSONString(appList));
                configService.removeConfig("chq-jc-gateway-"+app,nacosConfigProperties.getGroup());

            }

        } catch (NacosException e) {
            //ignore
        }

    }

    @Override
    public void onDelete(Object appid) {
        ConfigService configService = nacosConfigProperties.configServiceInstance();
        String app = String.valueOf(appid);
        List<String> appList = appService.listApps();

        try {
            configService.publishConfig("chq-jc-gateway-appid",nacosConfigProperties.getGroup(), JSONObject.toJSONString(appList));

            configService.removeConfig("chq-jc-gateway-"+app,nacosConfigProperties.getGroup());

        } catch (NacosException e) {
            //ignore
        }
    }
}
